﻿CREATE PROCEDURE [ApprovalManagement].[FollowUpEventsForUser]( @userGuid UNIQUEIDENTIFIER )
AS 
BEGIN

	SELECT 
		People.OID as 'PeopleOID'
		,People.Version as 'PeopleVersion'
		,People.FirstName 
		,People.LastName 
		,People.MiddleInitial 
		,People.DOB 
		,People.Sex 
		,People.Phone1 
		,People.Phone2 
		,People.eMailAddress
		,ServiceMenEvents.OID
		,ServiceMenEvents.Version
		,ServiceMen.PrimaryAddressID as ServiceMenPrimaryAddressID
		,ServiceMen.PermanentAddressID as ServiceMenPermanentAddressID
		,ActivityID
		,EventNotes
		,CreatedBy
		,CreatedOn
		,CompletedBy
		,CompletedOn
	FROM ServiceMenEvents
	INNER JOIN ServiceMen ON ServiceMen.OID = ServiceMenEvents.ServiceMenID
	INNER JOIN People ON People.OID = ServiceMen.OID
	WHERE ActivityID = 5 
	--AND ServiceMenEvents.CreatedBy = @userGuid
	
	UNION ALL
	
	SELECT 
		People.OID as 'PeopleOID'
		,People.Version as 'PeopleVersion'
		,People.FirstName 
		,People.LastName 
		,People.MiddleInitial 
		,People.DOB 
		,People.Sex 
		,People.Phone1 
		,People.Phone2 
		,People.eMailAddress
		,GrantEvents.OID
		,GrantEvents.Version
		,Applicants.AddressID as ServiceMenPrimaryAddressID
		,NULL as ServiceMenPermanentAddressID
		,ActivityID
		,EventNotes
		,CreatedBy
		,CreatedOn
		,CompletedBy
		,CompletedOn
	FROM GrantEvents
	INNER JOIN Grants ON Grants.OID = GrantEvents.GrantID
	INNER JOIN Applicants ON Applicants.OID = Grants.ApplicantID
	INNER JOIN People ON People.OID = Applicants.OID
	WHERE ActivityID = 5 
	--AND GrantEvents.CreatedBy = @userGuid
END